﻿#include "pch.h"
#include "ChangeProjectDlg.h"
#include "afxdialogex.h"
#include "ConfigFileManager.h"
#include "SystemManager.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

extern CSystemLog     gSystemLog;
extern CSystemManager    gSystemMgr;
extern CConfigFileManager gConfigMgr;

//extern HANDLE m_hSingleton;

IMPLEMENT_DYNAMIC(CChangeProjectDlg, CDialogEx)

CChangeProjectDlg::CChangeProjectDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_CHANGE_PROJECT, pParent)
{

}

CChangeProjectDlg::~CChangeProjectDlg()
{
}

void CChangeProjectDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT_CRT_PROJECT, m_editCrtProject);
	DDX_Control(pDX, IDC_LIST_PROJECT, m_editListProject);
}


BEGIN_MESSAGE_MAP(CChangeProjectDlg, CDialogEx)
	ON_LBN_DBLCLK(IDC_LIST_PROJECT, &CChangeProjectDlg::OnLbnDblclkListProject)
	ON_BN_CLICKED(IDOK, &CChangeProjectDlg::OnBnClickedOk)
END_MESSAGE_MAP()




BOOL CChangeProjectDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	ProjectInit();
	return TRUE;
}

void CChangeProjectDlg::ProjectInit()
{
	m_editCrtProject.SetWindowTextW(gSystemMgr.m_sGeneralSetting.m_cstrProjectName);

	CString cstrPath = gSystemMgr.GetCurrentExePath();
	cstrPath.Append(L"\\Recipe");
	vector<CString> vPath= GetFileFolderInPath(cstrPath);


	m_editListProject.ResetContent();
	for (int i=0;i<(int)vPath.size();i++)
	{
		Sleep(1);
		if (vPath[i]!=L"log")
		{
			m_editListProject.AddString(vPath[i]);
		}
	}

}

vector<CString> CChangeProjectDlg::GetFileFolderInPath(CString cstrPath)
{
	 vector<CString> vCstrPath;

	if (cstrPath == L"")
	{
		return vector<CString>();
	}

	if (cstrPath.Right(1) != _T("\\"))
	{
		cstrPath += L"\\";
		cstrPath = cstrPath + _T("*.*");
	}

	CFileFind cFinder;
	CString cstrPathChild;
	BOOL bWorking = cFinder.FindFile(cstrPath);
	while (bWorking)
	{
		Sleep(1);
		bWorking = cFinder.FindNextFile();
		cstrPathChild = cFinder.GetFilePath();
		//文件夹
		if (cFinder.IsDirectory() && !cFinder.IsDots())
		{
			CString cstrFolderName = cstrPathChild.Right(cstrPathChild.GetLength() - cstrPath.GetLength() + 3);
			vCstrPath.push_back(cstrFolderName);
		}
		//文件
		else if (!cFinder.IsDirectory() && !cFinder.IsDots())
		{
			CString cstrFolderName = cstrPathChild.Right(cstrPathChild.GetLength() - cstrPath.GetLength() + 3);
		}
	}
	return vCstrPath;
}

void CChangeProjectDlg::OnLbnDblclkListProject()
{
	int nSel = m_editListProject.GetCurSel();
	if (nSel<0)
	{
		return;
	}
	else
	{
		CString cstr;
		m_editListProject.GetText(nSel, cstr);
		if (IDYES == AfxMessageBox(L"确定切换型号吗？", MB_YESNO))
		{
			gSystemMgr.m_cGeneralSetting.SaveConfigIni(L"GeneralSetting", L"m_cstrProductName", cstr);

			CString cstrPath = gSystemMgr.GetCurrentExePath();
			cstrPath.Append(L"\\40GlueTest.exe");
			STARTUPINFO sinf;
			PROCESS_INFORMATION pif;
			memset(&sinf, 0, sizeof(STARTUPINFO));
			sinf.cb = sizeof(STARTUPINFO);
			sinf.wShowWindow = SW_SHOW;
			sinf.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
			bool bwork = ::CreateProcess((LPCTSTR)cstrPath, NULL, NULL, NULL, FALSE,
				0, NULL, NULL, &sinf, &pif);

			::TerminateProcess(::GetCurrentProcess(), NULL);//终止原先程序
			//ExitProcess(0);
		}
	}
}

void CChangeProjectDlg::OnBnClickedButtonSave()
{
}

void CChangeProjectDlg::OnCancel()
{

	CDialogEx::OnCancel();
}

BOOL CChangeProjectDlg::PreTranslateMessage(MSG* pMsg)
{
	WPARAM param = pMsg->wParam;
	if (param == VK_ESCAPE || param == VK_RETURN || param == VK_SPACE)
		return TRUE;
	if (pMsg->message == WM_SYSKEYDOWN && param == VK_F4)
		return TRUE;
	return CDialogEx::PreTranslateMessage(pMsg);
}


void CChangeProjectDlg::OnBnClickedOk()
{
	int nSel = m_editListProject.GetCurSel();
	if (nSel < 0)
	{
		return;
	}
	else
	{
		CString cstr;
		m_editListProject.GetText(nSel, cstr);
		if (IDYES == AfxMessageBox(L"确定切换型号吗？", MB_YESNO))
		{
			gSystemMgr.m_cGeneralSetting.SaveConfigIni(L"GeneralSetting", L"m_cstrProductName", cstr);

			/*CString cstrPath = gSystemMgr.GetCurrentExePath();
			cstrPath.Append(L"\\EightStationsAA.exe");*/

			/*		STARTUPINFO sinf;
					PROCESS_INFORMATION pif;
					memset(&sinf, 0, sizeof(STARTUPINFO));
					sinf.cb = sizeof(STARTUPINFO);
					sinf.wShowWindow = SW_SHOW;
					sinf.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
					bool bwork = ::CreateProcess((LPCTSTR)cstrPath, NULL, NULL, NULL, FALSE,
						0, NULL, NULL, &sinf, &pif);*/

			::TerminateProcess(::GetCurrentProcess(), NULL);//终止原先程序
			//ExitProcess(0);
		}
	}
	CDialogEx::OnOK();
}
